<?php
/**
*
* @filename 投资人调查
*/
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Invest as Invests;
use app\admin\model\Question;
use app\admin\model\Answer;
use think\Db;

class Invest extends Common
{
    private $cModel;   //当前控制器关联模型
    
    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Invests;   //别名：避免与控制名冲突
    }
    public function index(){
        $where = [];
        if (isset($_GET['is_check']) && input('get.is_check')!=-1){
            $where['is_check'] = ['eq', input('get.is_check')];
        }
        if (input('get.search')){
            if(is_numeric(trim(input('get.search')))){
                $where['id'] = ['eq', intval(input('get.search'))];
            }else{
                $where['title'] = ['like', '%'.input('get.search').'%'];
            }
        }
        
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'id desc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    public function create(){
        if (request()->isPost()){
            Db::startTrans();
            try{
                $data = input('post.');
                $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
                $uid = $this->cModel->getLastInsID();
                // 提交事务
                if ($result && $uid){
                    Db::commit();
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajaxReturn($e->getMessage());
            }
        }else{
            return $this->fetch('create');
        }
    }

    public function edit($id){
        if (request()->isPost()){
            $data = input('post.');
			$result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->allowField(true)->save($data,$data['id']);
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            if ($id > 0){
                $qModel = new Question();
                $data = $qModel->where(['jid' => $id])->paginate(15);
                $this->assign('data', $data);
                $this->assign('jid', $id);
                return $this->fetch();
            }
        }
    }
    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                Db::startTrans();
                try{
                    $id_arr = explode(',', $id);   //问卷ID
					$qModel = new question();
					$aModel = new Answer();
                    $where1 = [ 'id' => ['in', $id_arr] ];
                    $where2 = [ 'jid' => ['in', $id_arr] ];
                    $result = $this->cModel->where($where1)->delete();   //删除问卷
                    $result2 = $qModel->where($where2)->delete();   //删除问题
                    $result3 = $aModel->where($where2)->delete();   //删除答案
                    // 提交事务
                    if ($result){
                        Db::commit();
                        return ajaxReturn(lang('action_success'), url('index'));
                    }else{
                        return ajaxReturn($this->cModel->getError());
                    }
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return ajaxReturn($e->getMessage());
                }
            }
        }
    }
}